#include "Bsp_CAN.h"

CAN_TxHeaderTypeDef   TxHeader;
CAN_RxHeaderTypeDef   RxHeader;


//void CAN_filter_init(void)        //can�?滤�?��??�??
//{
//    CAN_FilterTypeDef  can_filter;/*�?滤�?��?��??*/
//
//    can_filter.FilterBank           = 0;
//    /*使�?��??�?滤�?��??��??使�?��?�?CAN�????????0~13�?使�?�两�?CAN?????0~27*/
//    can_filter.FilterMode           = CAN_FILTERMODE_IDMASK;
//    /*�?滤�?�模�?????��???��??模�?�???CAN_FILTERMODE_IDMASK�?
//                      ??表模�?�???CAN_FILTERMODE_IDLIST */
//    can_filter.FilterScale          = CAN_FILTERSCALE_32BIT;
//    /*�?滤�?��?宽�?32�???CAN_FILTERSCALE_32BIT�?
//                  16�???CAN_FILTERSCALE_16BIT */
//    can_filter.FilterIdHigh         = 0x0000;
//    /*�?滤�?��??�???ID�?16�?�????��??:0~0xFFFF*/
//    can_filter.FilterIdLow          = 0x0000;
//    /*�?滤�?��??�???ID�?16�?�????��??:0~0xFFFF*/
//    can_filter.FilterMaskIdHigh     = 0x0000;
//    /*�?滤�?��?��??ID�?16�?�????��??:0~0xFFFF*/
//    can_filter.FilterMaskIdLow      = 0x0000;
//    /*�?滤�?��?��??ID�?16�?�????��??:0~0xFFFF*/
//    can_filter.FilterFIFOAssignment = CAN_FilterFIFO0;
//    /*�????�????��???��?��??�?FIFOx�?�?�???FIFO(x)*/
//    can_filter.SlaveStartFilterBank = 0;
//    /*can2�?�???�?滤�??*/
//    can_filter.FilterActivation     = ENABLE;
//    /*????使�?��?滤�??�?DISABLE ?? ENABLE */
//
//    HAL_CAN_ConfigFilter(&hcan1, &can_filter);
//    while (HAL_CAN_ConfigFilter(&hcan1, &can_filter) != HAL_OK);
//    /*??�?can1�?滤�?�并�?�??��??�?�???*/
//
//    can_filter.FilterBank           = 14;
//    HAL_CAN_ConfigFilter(&hcan2, &can_filter);
//    while (HAL_CAN_ConfigFilter(&hcan2, &can_filter) != HAL_OK);
//    /*??�?can2�?滤�?�并�?�??��??�?�???*/
//
//    HAL_Delay(100);
//    HAL_CAN_Start(&hcan1);
//    HAL_CAN_ActivateNotification(&hcan1,CAN_IT_RX_FIFO0_MSG_PENDING);
//    /*�???can1并使?�中??*/
//    HAL_CAN_Start(&hcan2);
//    HAL_CAN_ActivateNotification(&hcan2,CAN_IT_RX_FIFO0_MSG_PENDING);
//    /*�???can2并使?�中??*/
//
//}
void CAN_Send_Msg(CAN_HandleTypeDef *hcan,
                  uint16_t CANID,
                  uint8_t *msg,
                  uint8_t len,
                  uint8_t Frame_type,
                  uint8_t ID_type)
{
    uint32_t TxMailbox = CAN_TX_MAILBOX0;

    TxHeader.IDE=ID_type; //CAN_ID_STD ? CAN_ID_EXT
    if (ID_type == CAN_ID_STD) {
        TxHeader.StdId =CANID;
    } else
        TxHeader.ExtId=CANID;
    TxHeader.RTR = Frame_type != CAN_RTR_REMOTE ? CAN_RTR_DATA : CAN_RTR_REMOTE;
    TxHeader.DLC=len;

    uint8_t send_buf[8] = {0};
    for (uint8_t index = 0; index < len; index++) {
            send_buf[index] = msg[index];
    }

    while(HAL_CAN_GetTxMailboxesFreeLevel(hcan) == 0)

    if ((hcan->Instance->TSR & CAN_TSR_TME0) != RESET) {
        // 检查发送邮箱0状态 如果邮箱0空闲就将待发送数据放入FIFO0
        TxMailbox = CAN_TX_MAILBOX0;
    } else if ((hcan->Instance->TSR & CAN_TSR_TME1) != RESET) {
        TxMailbox = CAN_TX_MAILBOX1;
    } else if ((hcan->Instance->TSR & CAN_TSR_TME2) != RESET) {
        TxMailbox = CAN_TX_MAILBOX2;
    }

    if(HAL_CAN_AddTxMessage(hcan, &TxHeader, send_buf, &TxMailbox) != HAL_OK)
    {
        Error_Handler();
    }
}

void CAN_Receive_Msg(CAN_HandleTypeDef *hcan,uint8_t *buf)
{
    uint8_t	RxData[8];

    if(HAL_CAN_GetRxMessage(hcan, CAN_RX_FIFO0, &RxHeader, RxData) != HAL_OK)
    {
        Error_Handler();
    }

    for (uint8_t i = 0; i < RxHeader.DLC; i++) {
        buf[i]=RxData[i];
    }
}


//*
//* @brief CANFifo0的接收回调函数,用于更改DJI电驱接收ID
//* @param[in]... 更改DJI电调ID 0x20+电机编号
// */
//void HAL_CAN_RxFifo0MsgPendingCallback(CAN_HandleTypeDef *hcan) {
//    if (hcan->Instance == CAN1) {
//        uint8_t RxBuff[8];
//        HAL_CAN_GetRxMessage(&hcan1,CAN_FILTER_FIFO0,&RxHeader,RxBuff);
//        }
//
//
//
//    if (hcan->Instance == CAN2) {
//
//        }
//}
/*
 * @brief CANFif1的接收回调函数,用于更改DJI电驱接收ID
 * @param[in]... 更改DJI电调ID 0x20+电机编号
 */
void HAL_CAN_RxFifo1MsgPendingCallback(CAN_HandleTypeDef *hcan) {
    if (hcan->Instance == CAN1) {

    }

    if (hcan->Instance == CAN2) {

    }
}